/**
 * describe:
 *
 * @author chaP
 * @date 2018/12/24
 */
package CodingTest;
/**
 * 题目描述： 输入两个链表，找出它们的第一个公共结点
 */

public class publicNode {
    public ListNode FindFirstCommonNode(ListNode pHead1,ListNode pHead2){
        ListNode tmp1 = pHead1;
        ListNode tmp2 = pHead2;
        int size1 = 0;
        int size2 = 0;
        while(tmp1 != null){
         size1 ++;
         tmp1 = tmp1.next;
        }
        while(tmp2 != null){
            size2++;
            tmp2 = tmp2.next;
        }
        tmp1 = pHead1;
        tmp2 = pHead2;
        if(size1 >size2){
            int p = size1 - size2;
            for(;p > 0; p--){
                tmp1 = tmp1.next;
            }
        }else{
            int p = size2 - size1;
            for(;p>0;p--){
                tmp2 = tmp2.next;
            }
        }
        while(tmp1 != tmp2 && tmp1 != null&& tmp2!=null){
            tmp1 = tmp1.next;
            tmp2 = tmp2.next;
        }
        return tmp1;
    }
}
